<html lang="en">
	<head>
		<title>Yuka | Steering Behaviors | Flee</title>
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
		<link rel="stylesheet" type="text/css" href="../../lib/styles.css">
		<link rel="shortcut icon" type="image/x-icon" href="https://mugen87.github.io/yuka/favicon.ico">
	</head>
<body>

	<section id="info">
		<p>
			This steering behavior produces a force that steers an agent away from a target position. <br/>
			The target position is defined by the mouse cursor.
		</p>
	</section>

	<script type="module">

		import * as YUKA from '../../../build/yuka.module.js';
		import * as THREE from 'https://cdn.jsdelivr.net/npm/three@0.109/build/three.module.js';

		let renderer, scene, camera;
		let pointer, raycaster, plane;

		let entityManager, time, vehicle, target;

		init();
		animate();

		function init() {

			scene = new THREE.Scene();

			pointer = new THREE.Vector2( 1, 1 );

			camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 0.1, 100 );
			camera.position.set( 0, 10, 0 );
			camera.lookAt( scene.position );
			camera.updateMatrixWorld();

			//

			const vehicleGeometry = new THREE.ConeBufferGeometry( 0.1, 0.5, 8 );
			vehicleGeometry.rotateX( Math.PI * 0.5 );
			const vehicleMaterial = new THREE.MeshNormalMaterial();

			const vehicleMesh = new THREE.Mesh( vehicleGeometry, vehicleMaterial );
			vehicleMesh.matrixAutoUpdate = false;
			scene.add( vehicleMesh );

			const grid = new THREE.GridHelper( 10, 25 );
			scene.add( grid );

			//

			raycaster = new THREE.Raycaster();

			plane = new THREE.Plane();
			plane.normal.set( 0, 1, 0 );

			renderer = new THREE.WebGLRenderer( { antialias: true } );
			renderer.setPixelRatio( window.devicePixelRatio );
			renderer.setSize( window.innerWidth, window.innerHeight );
			document.body.appendChild( renderer.domElement );

			//

			document.addEventListener( 'mousemove', onPointerMove, false );
			document.addEventListener( 'touchmove', onPointerMove, false );
			document.addEventListener( 'touchstart', onPointerMove, false );

			window.addEventListener( 'resize', onWindowResize, false );

			// game setup

			target = new YUKA.Vector3();

			entityManager = new YUKA.EntityManager();
			time = new YUKA.Time();
			vehicle = new YUKA.Vehicle();
			vehicle.setRenderComponent( vehicleMesh, sync );

			const fleeBehavior = new YUKA.FleeBehavior( target, 5 );
			vehicle.steering.add( fleeBehavior );

			entityManager.add( vehicle );

		}

		function onWindowResize() {

			camera.aspect = window.innerWidth / window.innerHeight;
			camera.updateProjectionMatrix();

			renderer.setSize( window.innerWidth, window.innerHeight );

		}

		function onPointerMove( event ) {

			const clientX = event.clientX || event.touches[ 0 ].clientX;
			const clientY = event.clientY || event.touches[ 0 ].clientY;

			pointer.x = ( clientX / window.innerWidth ) * 2 - 1;
			pointer.y = - ( clientY / window.innerHeight ) * 2 + 1;

		}

		function animate() {

			requestAnimationFrame( animate );

			raycaster.setFromCamera( pointer, camera );
			raycaster.ray.intersectPlane( plane, target );

			const delta = time.update().getDelta();

			entityManager.update( delta );

			renderer.render( scene, camera );

		}

		function sync( entity, renderComponent ) {

			renderComponent.matrix.copy( entity.worldMatrix );

		}


	</script>

</body>
</html>
